package com.chuanyang.driverh.mainhome.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.mainhome.MotorcadeMsgActivity;
import com.chuanyang.driverh.mainhome.adapter.AddAdapter;
import com.chuanyang.driverh.mainhome.adapter.AllAdapter;
import com.chuanyang.driverh.mainhome.adapter.RecommendAdapter;
import com.chuanyang.driverh.response2.join.ResultAddList;
import com.chuanyang.driverh.response2.join.ResultFleetJoinTop;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.StringUtil;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;
import com.chuanyang.driverh.utils.widget.MyListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

public class DriverJoinFragment extends Fragment implements View.OnClickListener {

    private MyListView lv_recently_add,lv_all_add;

    private TextView tv_jiameng_fleet;
    private TextView tv_jiedan_num;

    private RelativeLayout rl_ad;
    private ImageView iv_close_ad;
    private AddAdapter addAdapter;
    private AllAdapter allAdapter;

    private View view_add_foot;
    private TextView tv_add_more;
    private int page = 1;
    private List<ResultAddList.DataBeanX.DataBean> dataBeanList = new ArrayList<>();

    private List<ResultAddList.DataBeanX.DataBean> dataBeanListrecently = new ArrayList<>();

    private List<ResultAddList.DataBeanX.DataBean> dataBeanListrecommend = new ArrayList<>();

    private MyListView lv_recommend;
    private RecommendAdapter recommendAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_driver_join, container, false);
        lv_recently_add = (MyListView) view.findViewById(R.id.lv_recently_add);
        lv_all_add = (MyListView) view.findViewById(R.id.lv_all_add);

        tv_jiameng_fleet = (TextView) view.findViewById(R.id.tv_jiameng_fleet);
        tv_jiedan_num = (TextView) view.findViewById(R.id.tv_jiedan_num);

        rl_ad = (RelativeLayout) view.findViewById(R.id.rl_ad);
        iv_close_ad = (ImageView) view.findViewById(R.id.iv_close_ad);
        iv_close_ad.setOnClickListener(this);

        view_add_foot = inflater.inflate(R.layout.add_item_foot,null);
        tv_add_more = (TextView) view_add_foot.findViewById(R.id.tv_add_more);
        tv_add_more.setOnClickListener(this);
        lv_recommend = (MyListView) view.findViewById(R.id.lv_recommend);
        lv_recently_add.addFooterView(view_add_foot);
        lv_recently_add.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), MotorcadeMsgActivity.class);
                intent.putExtra("data", "" + dataBeanListrecently.get(position).getFleetId());
                startActivity(intent);
            }
        });
        addAdapter = new AddAdapter(getActivity());
        lv_recently_add.setAdapter(addAdapter);

        allAdapter = new AllAdapter(getActivity());
        allAdapter.setNewData(dataBeanList);
        lv_all_add.setAdapter(allAdapter);
        lv_all_add.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), MotorcadeMsgActivity.class);
                intent.putExtra("data", "" + dataBeanList.get(position).getFleetId());
                startActivity(intent);
            }
        });

        recommendAdapter = new RecommendAdapter(getActivity());
        lv_recommend.setAdapter(recommendAdapter);;
        lv_recommend.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), MotorcadeMsgActivity.class);
                intent.putExtra("data", "" + dataBeanListrecommend.get(position).getFleetId());
                startActivity(intent);
            }
        });
        getTopData();
        //获取列表的信息
        //1，最近添加
        getRecentlyData();
        //2，所有添加
        getAllData(page);
        //3，获取推荐的
        getRecommendData(1);

        return view;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_close_ad:
                rl_ad.setVisibility(View.GONE);
                break;
            case R.id.tv_add_more:
                //查看更多
                page = page+1;
                getAllData(page);
                break;
        }
    }


    private void getTopData() {
        DialogUtil.showLoadingDialog(getActivity(),"加载中...");
        Map<String,Object> params=new HashMap<String, Object>();
        params.put("type", "1");
        params.put("driverId", SharedPrefUtil.getUserId());
        //params.put("fleetId", SharedPrefUtil.getFleetId());
        MyHttpUtil.getBusinessToJoin(getActivity(),params, new MyCallback<ResultFleetJoinTop>(getActivity(),
                new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
                //springView.onFinishFreshAndLoad();
            }
            @Override
            public void onSuccess(ResultFleetJoinTop result, int id) {
                if (result.getCode() == ConstUtil.OK ) {
                    try {
                        tv_jiedan_num.setText(result.getData().getJoinedNum()+"");
                        tv_jiameng_fleet.setText(result.getData().getFleetMemberNum()+"");
                    } catch (Exception e) {
                    }
                }else{
                    if (!StringUtil.isBlank(result.getMsg()))
                        PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });
    }
    private void getRecentlyData() {
        DialogUtil.showLoadingDialog(getActivity(),"加载中...");
        Map<String,Object> params=new HashMap<String, Object>();
        params.put("length", "3");
        params.put("driverId", SharedPrefUtil.getUserId());
        params.put("page", "1");
        MyHttpUtil.selectBusinessByDriverInfo(getActivity(),params, new MyCallback<ResultAddList>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
                //springView.onFinishFreshAndLoad();
            }
            @Override
            public void onSuccess(ResultAddList result, int id) {

                if (result.getCode() == ConstUtil.OK ) {
                    try {
                        dataBeanListrecently.addAll(result.getData().getData());
                        addAdapter.setNewData(dataBeanListrecently);
                    } catch (Exception e) {
                    }
                }else{
                    if (!StringUtil.isBlank(result.getMsg()))
                        PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });
    }
    private void getAllData(int page) {
        DialogUtil.showLoadingDialog(getActivity(),"加载中...");
        Map<String,Object> params=new HashMap<String, Object>();
        params.put("length", "4");
        params.put("driverId", SharedPrefUtil.getUserId());
        params.put("page", page);
        MyHttpUtil.selectBusinessByDriverInfo(getActivity(),params, new MyCallback<ResultAddList>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
                //springView.onFinishFreshAndLoad();
            }
            @Override
            public void onSuccess(ResultAddList result, int id) {

                if (result.getCode() == ConstUtil.OK ) {
                    try {
                        dataBeanList.addAll(result.getData().getData());
                        allAdapter.setNewData(dataBeanList);
                        if(dataBeanList.size() == 0){
                            lv_all_add.removeFooterView(view_add_foot);
                        }
                    } catch (Exception e) {
                    }
                }else{
                    if (!StringUtil.isBlank(result.getMsg()))
                        PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });
    }

    private void getRecommendData(int page) {
        DialogUtil.showLoadingDialog(getActivity(),"加载中...");
        Map<String,Object> params=new HashMap<String, Object>();
        params.put("length", "10");
        params.put("driverId", SharedPrefUtil.getUserId());
        params.put("page", page);
        MyHttpUtil.selectRecommendBusiness(getActivity(),params, new MyCallback<ResultAddList>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
                //springView.onFinishFreshAndLoad();
            }
            @Override
            public void onSuccess(ResultAddList result, int id) {

                if (result.getCode() == ConstUtil.OK ) {
                    try {
                        dataBeanListrecommend.addAll(result.getData().getData());
                        recommendAdapter.setNewData(dataBeanListrecommend);
                    } catch (Exception e) {
                    }
                }else{
                    if (!StringUtil.isBlank(result.getMsg()))
                        PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });
    }
}
